package com.xbongbong.crm.perform.query.render.edit;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.crm.abstracts.AbstractCrmEditPageRenderService;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.pojo.dto.FormDataUpdateGetDTO;
import com.xbongbong.paas.pojo.vo.ParseSingleRowDataVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.pro.businessdata.pojo.dto.FormatExplainDTO;
import com.xbongbong.pro.formdata.pojo.dto.SaasAnalysisDataDTO;
import com.xbongbong.pro.formexplain.pojo.vo.HandlerExplainVO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.CustomerCommunicateEnum;
import com.xbongbong.saas.enums.business.FundTransferEnum;
import com.xbongbong.saas.model.CustomerCommunicateModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;

/**
 * @author sunshaoxiang
 * @ClassName CustomerCommunicateEditPageRenderServiceImpl
 * @Description 跟进记录 update get 业务层
 * @Date 2022/6/2 10:33
 * @Version 1.0
 **/
@Service("customerCommunicateEditPageRenderServiceImpl")
public class CustomerCommunicateEditPageRenderServiceImpl extends AbstractCrmEditPageRenderService {
    private static final Logger LOGGER = LoggerFactory.getLogger(CustomerCommunicateEditPageRenderServiceImpl.class);

    @Resource
    private CustomerCommunicateModel customerCommunicateModel;

    @Override
    public List<Integer> businessType() {
        return Collections.singletonList(XbbRefTypeEnum.CUSTOMER_COMMUNICATE.getCode());
    }

    @Override
    public ParseSingleRowDataVO formatData(FormDataUpdateGetDTO formDataUpdateGetDTO, HandlerExplainVO handlerExplainVO, PaasFormDataEntityExt paasFormDataEntityExt) throws XbbException {
        SaasAnalysisDataDTO saasAnalysisDataDTO = new SaasAnalysisDataDTO();
        BeanUtil.copyProperties(formDataUpdateGetDTO, saasAnalysisDataDTO);
        // saas特殊格式转换
        JSONObject data = paasFormDataEntityExt.getData();
        Map<String, FieldAttrEntity> explainMap = ExplainUtil.getExplainMapByList(handlerExplainVO.getHeadList());
        saasAnalysisDataDTO.setExplainMap(explainMap);
        customerCommunicateModel.formatData(saasAnalysisDataDTO, data);
        //过滤一个数据字段
        if (data.containsKey(CustomerCommunicateEnum.AT_USERNAME.getAttr())) {
            JSONArray jsonArray = data.getJSONArray(CustomerCommunicateEnum.AT_USERNAME.getAttr());
            if (jsonArray.size() <= 0) {
                data.remove(CustomerCommunicateEnum.AT_USERNAME.getAttr());
            }
        }
        data.put(FieldTypeEnum.OWNERID.getAlias(), handlerExplainVO.getOwnerId());
        data.put(FieldTypeEnum.COUSERID.getAlias(), handlerExplainVO.getCoUserId());
        data.put(FieldTypeEnum.DEPARTMENTID.getAlias(), paasFormDataEntityExt.getDepartmentId());
        data.put(FieldTypeEnum.SERIALNO.getAlias(), paasFormDataEntityExt.getSerialNo());
        ParseSingleRowDataVO parseSingleRowDataVO = parseSingleRowData(formDataUpdateGetDTO, data, explainMap, handlerExplainVO.getUserMap(), handlerExplainVO.getDepartmentMap());
        paasFormDataEntityExt.setData(parseSingleRowDataVO.getResult());
        return parseSingleRowDataVO;
    }

    @Override
    public void businessHandleExplain(FormDataUpdateGetDTO formDataUpdateGetDTO, List<FieldAttrEntity> explainList, PaasFormEntityExt form, PaasFormDataEntityExt paasFormDataEntityExt) throws XbbException {
        boolean formCopy = Objects.nonNull(formDataUpdateGetDTO.getSaasSpecialParamPojo()) && Objects.equals(formDataUpdateGetDTO.getSaasSpecialParamPojo().getFromCopy(), BasicConstant.ONE);
        handelSaasSpecialUpdate(explainList, form.getBusinessType(), paasFormDataEntityExt, formCopy, form.getDistributorMark());

        // 编辑时saas字段显示特殊逻辑
        FormatExplainDTO formatExplainDTO = new FormatExplainDTO();
        BeanUtil.copyProperties(formDataUpdateGetDTO, formatExplainDTO);
        formatExplainDTO.setExplainList(explainList);
        formatExplainDTO.setDataEntity(paasFormDataEntityExt);
        formatExplainDTO.setResult(paasFormDataEntityExt.getData());
        formatExplainDTO.setFromCopy(formCopy);
        customerCommunicateModel.formatUpdateExplain(formatExplainDTO);

    }

    @Override
    public void handelSaasSpecialUpdate(List<FieldAttrEntity> explainList, Integer businessType, PaasFormDataEntityExt formDataEntityExt, boolean formCopy, Integer distributorMark) throws XbbException {

        if (!formCopy) {
            // 跟进记录编辑不需要下次跟进提醒相关的字段
            explainList.removeIf(item -> Objects.equals(item.getAttr(), CustomerCommunicateEnum.IF_REMIND.getAttr()) ||
                    Objects.equals(item.getAttr(), CustomerCommunicateEnum.NEXT_COMMUNICATE_TIME.getAttr()) ||
                    Objects.equals(item.getAttr(), CustomerCommunicateEnum.NOTIFY_TYPE.getAttr()) ||
                    Objects.equals(item.getAttr(), CustomerCommunicateEnum.NOTIFY_TIME.getAttr()) ||
                    Objects.equals(item.getAttr(), CustomerCommunicateEnum.NEXT_NOTIFY_MEMO.getAttr()) ||
                    Objects.equals(item.getAttr(), CustomerCommunicateEnum.ENTER_COMMUNICATE_PLAN.getAttr()) ||
                    Objects.equals(item.getAttr(), CustomerCommunicateEnum.FINISH_COMMUNICATE_PLAN_ID.getAttr())
            );
        }
    }
}
